<template>
    <el-empty description="您还未登录" v-if="token == null"></el-empty>
    <div v-else>
        <el-table :data="tableData" style="width: 100%;margin-top: 20px;">
            <el-table-column prop="student.name" label="我的训练" width="180" />
            <el-table-column prop="session" width="180" />
            <el-table-column prop="test.subject_name" width="180" />
            <el-table-column prop="car.car_pai" width="180" />
            <el-table-column prop="car.car_pai">
                <template #default="{ row }">
                    <el-button plain @click="sing(row.train_id)" v-if="row.is_status==false">
                        签到
                    </el-button>
                    <el-button plain @click="out(row.train_id)" v-else-if="row.is_status==true">
                        签退
                    </el-button>
                    <el-button plain v-else-if="row.is_status==null">
                        已签退
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>

</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
import axios from 'axios';
import { ElMessage, ElMessageBox } from 'element-plus'
const token = localStorage.getItem('token')
// 创建Date对象
const currentDate = new Date();

// 获取年月日并处理格式
const year = currentDate.getFullYear();
const month = String(currentDate.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要+1
const day = String(currentDate.getDate()).padStart(2, '0');

// 组合成YYYY-MM-DD格式
const session = `${year}-${month}-${day}`;
console.log(session);
const tableData = ref([])
function getdata() {
    axios.get("http://localhost:8000/driver_school/train_list", {
        headers: {
            token: token,
        }
    })
        .then(res => {
            console.log(res)
            tableData.value = res.data.data
        })
        .catch(err => {
            console.error(err);
        })
}
function sing(train_id) {
    const data = ref({
        is_status: 1
    })
    axios.put("http://localhost:8000/driver_school/train_change/" + train_id, data.value, { headers: { token: token } })
        .then(res => {
            console.log(res)
            if (res.data.code==200){
                ElMessage("签到成功")
            }
            getdata()
        })
        .catch(err => {
            console.error(err);
        })
}
function out(train_id){
    const data = ref({
        is_status: null
    })
    axios.post("http://localhost:8000/driver_school/train_out/" + train_id, data.value, { headers: { token: token } })
        .then(res => {
            console.log(res)
            if (res.data.code==200){
                ElMessage("签退成功")
            }
            getdata()
        })
        .catch(err => {
            console.error(err);
        })
}
onMounted(() => {
    // Your code here
    getdata()
});

// 在组件卸载前执行的操作
onUnmounted(() => {
    // Your code here
});
</script>